#!/usr/bin/env python
#coding=utf-8

import urllib2
import json
import re
from pprint import pprint

def get_job_detail(job_id):
    url = "http://192.168.3.171:8088/proxy/application_%s/ws/v1/mapreduce/jobs" % job_id
    html = fetchurl_json(url)
    if html is None:
        return None
    job = html['jobs']['job'][0]
    return job

def fetchurl(url):
    response = urllib2.urlopen(url)
    html = response.read()
    # print html
    return html

def fetchurl_json(url):
    class NoRedirection(urllib2.HTTPErrorProcessor):
        def http_response(self, request, response):
            return response
        https_response = http_response
    opener = urllib2.build_opener(NoRedirection)
    opener.addheaders = [('Accept', 'application/json')]
    response = opener.open(url)
    if response.code == 302:
        print "302"
        return None
    html = response.read()
    html = json.loads(html)
    return html

def get_jobIds():
    url = "http://192.168.3.171:8088/cluster/apps/RUNNING"
    html = fetchurl(url)
    idx_start = html.find("var appsTableData=")
    data = html[idx_start:]
    idx_end = data.find("</script>")
    data = data[:idx_end]

    job_short_ids = []
    for line in data.split("\n"):
        m = re.match(".*application_([0-9]+_[0-9]+)", line)
        if m:
            job_short_ids.append(m.group(1))

    return job_short_ids

def get_jobs():
    jobs = []
    jobIds = get_jobIds()

    for job in jobIds:
        # print "=" * 80
        # print "http://192.168.3.171:8088/proxy/application_%s/" %  job
        jobDetail = get_job_detail(job)
        # pprint(jobDetail)
        # pprint(jobDetail)
        if jobDetail is not None:
            # print "mapsTotal=%s, mapsCompleted=%s, reducesTotal=%s, reducesCompleted=%s, user=%s" % (
            #     jobDetail['mapsTotal'],
            #     jobDetail['mapsCompleted'],
            #     jobDetail['reducesTotal'],
            #     jobDetail['reducesCompleted'],
            #     jobDetail['user'])
            jobDetail['job_num']=job
            jobs.append(jobDetail)
            # pprint(jobDetail)
        else:
            print "closed job "
    return jobs